//
//  TabBarView.swift
//  TabBarView
//
//  Created by haisheng huang on 15/10/31.
//  Copyright © 2015年 huanghaisheng. All rights reserved.
//

import UIKit

@objc public protocol TabBarViewDelegate: NSObjectProtocol {

    optional func didSelectAtIndex(index: Int, view: TabBarView)
    optional func shouldSelectAtIndex(index: Int, view: TabBarView) -> Bool
}

@objc public class TabBarView: UIView, TabBarItemViewDelegate  {

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

    var tarBarInfos: NSArray?
    var topLineView: UIImageView?
    var selectedTabBarItem: TabBarItemView?
    var delegate: TabBarViewDelegate?
    var selectedIndex: Int?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        self.delegate = nil
    }
    
    public init(frame: CGRect, items: NSArray, barStyle: TabItemStyle, selectedIndex: Int) {
        
        super.init(frame: frame)
        self.backgroundColor = UIColor.whiteColor()
        self.loadCustomTabBarView(items, barStyle: barStyle, selectedIndex: selectedIndex)
        
    }
    
    public convenience init(frame: CGRect, items: NSArray, barStyle: TabItemStyle, selectedIndex: Int, test:Bool) {
        
        self.init(frame: frame)
        self.backgroundColor = UIColor.whiteColor()
        self.loadCustomTabBarView(items, barStyle: barStyle, selectedIndex: selectedIndex)
    }
    
    /*检测选择中的index是否有效，无效则重置为0*/
    private func validDateIndex(var index: Int) -> Int {
        
        if (index < 0) || (index >= self.tarBarInfos?.count) {
            index = 0
        }
        return index
    }
    
    public func shouldClickedTabBarItemView(view: TabBarItemView) -> Bool {
        
        if view .isEqual(self.selectedTabBarItem) {
            return false
        } else {
            if ((self.delegate?.respondsToSelector("shouldSelectAtIndex:")) != false) {
                return (self.delegate?.shouldSelectAtIndex!(view.tag, view: self))!
            } else {
                return true
            }
            
        }
    }
    
    public func didClickedTabBarItemView(view: TabBarItemView) {
        
        if view .isEqual(self.selectedTabBarItem) {
            return
        } else {
            self.selectedTabBarItem?.setNormalStatus()
            self.selectedTabBarItem = view
            self.delegate?.didSelectAtIndex!(view.tag, view: self)
        }
    }
    
    public func setBarItemHighlightedAtIndex(index: Int) {
        
        for itemView in self.subviews {
            
            if itemView is TabBarItemView {
                
                if itemView.tag == index {
                    (itemView as! TabBarItemView).setHighlightedStatus()
                    self.selectedTabBarItem = itemView as? TabBarItemView
                } else {
                    (itemView as! TabBarItemView).setNormalStatus()
                }
            }
        }
    }
    
    public func clickAtIndex(index: Int) {
    
        for itemView in self.subviews {
            
            if itemView is TabBarItemView {
                
                if itemView.tag == index {
                    (itemView as! TabBarItemView).clickMe()
                }
            }
        }
    }
    
    public func descriptionThisView() {
        
        print("TabBarView")
    }
    
    public func loadCustomTabBarView(items: NSArray, barStyle: TabItemStyle, selectedIndex: Int) {
        
        self.tarBarInfos = items
        self.selectedIndex = selectedIndex
        
        /*加顶部灰色线*/
        let topLineView = UIView(frame: CGRectMake(0.0, 0.0, frame.width, 0.5))
        topLineView.backgroundColor = UIColor(white: 216.0/255.0, alpha: 1.0)
        self.addSubview(topLineView)
        
        let itemWidth:CGFloat = frame.width/CGFloat(items.count)
        /*添加Bar Item*/
        for i in 0...items.count-1 {
            
            let tempObj = items[i]
            let itemView: TabBarItemView
            let itemFrame: CGRect = CGRectMake(itemWidth*CGFloat(i), 0.0, itemWidth, frame.height)
            
            if tempObj is TabBarItemView {
                itemView = tempObj as! TabBarItemView
                itemView.frame = itemFrame
            } else {
                itemView = TabBarItemView(frame: itemFrame, itemInfo: tempObj as? NSDictionary, itemStyle: barStyle)
            }
            itemView.delegate = self
            itemView.tag = i
            self.addSubview(itemView)
            
            if i == selectedIndex {
                self.selectedTabBarItem = itemView
                itemView.setHighlightedStatus()
            }
        }
    }
    
}
